import type { Router } from 'vue-router'
import { useSettingsStore } from '/@/store/modules/settings.ts'
import pinia from '/@/store'
import { titleReverse, titleSeparator } from '/@lib/config'

export function setupDocumentTitleGuard(router: Router) {
  router.afterEach((to) => {
    if (typeof to.meta.title === 'string') document.title = getPageTitle(to.meta.title)
  })

  const getPageTitle = (pageTitle: string) => {
    const { getTitle } = useSettingsStore(pinia)
    let newTitles = []
    // if (pageTitle) newTitles.push(translate(pageTitle))
    if (pageTitle) newTitles.push(pageTitle)
    if (getTitle) newTitles.push(getTitle)
    if (titleReverse) newTitles = newTitles.reverse()
    return newTitles.join(titleSeparator)
  }
}
